<?php

class TopicModel
{
	private $topicKeyPrefix  	=   'TP/';
	private $topicFavoriteKeyPrefix =   'CTP/';
	private $topicCommentsKeyPrefix  =   'TPC/';
	private $topicVoteLink 		 = 'TVL/';
	private $topicVoteOptions 	 = 'TVO/';
	private $topicVoteLogs 		 = 'TVG/';
	private $topicCountWeight	 = 'TCW/';
	private $slaveKey               =   '/slave';
	private $diaryListCacheModel;
	private $groupInfoModel;
	private $attachmentModel;

    const RECOMMEND_TPOIC_RETURN_MAX_LEN = 300;
	
	public function __construct(){
		$this->diaryListCacheModel      =   new DiaryListCacheModel();
		$this->groupInfoModel       	=   new GroupInfoModel();
		$this->attachmentModel          =   new AttachmentModel();
	}
	
	/**
	 * 发表话题
	 * @param unknown $info
	 * @return boolean|Ambigous <unknown, boolean>
	 */
	public function addTopic($info)/*{{{*/
	{	
		$data    =    array();
		$data['title']           =   $info['title'];
		$data['uid']	         =   $info['uid'];
		$data['gid']	         =   $info['gid'];
		$data['time']	         =   AC::SERVER_TIME;
		$data['category']        =   '0';
		$data['vote'] 		     =   $info['vote'];
		$data['abbreviation']    =   UK::getstr($info['content'], AC::TOPIC_ABBRE_LENGTH, '...',1);
		
		if (intval($info['category']) > 0){
		    $data['category']    =   intval($info['category']);
		}
		// 将最新发布时间加入最后回复进行排序
		$data['last_reply']      =   AC::SERVER_TIME;
		
		// 时间设置
		if (isset($info['time']))
		{
			$data['last_reply']		=	$data['time']	=	$info['time'];
		}
		
		isset($info['recommend'])    &&    $data['recommend']    =    $info['recommend'];
			
		$key    =   $this->topicKeyPrefix. 0;
		$result =   Storage::addData($key, $data);
		if (empty($result))
		{
			AC::$errorNo    =   'TPM002';
			return  FALSE;
		}
		
		return  $result;
	}/*}}}*/
	
	/**
     * 添加投票关系表
     * @param unknown $info            
     * @return boolean
     */
    public function addVoteLink($info)
    {
        if (empty($info['tid'])) {
            return false;
        }
        
        $data = array(
            'tid'           =>  $info['tid'],
            'type'          =>  $info['type'],
            'max_options'   =>  $info['max_options'],
            'dateline'      =>  $info['dateline'],
            'time'          =>  AC::SERVER_TIME,
            'status'        =>  0,
            'extra'         =>  $info['extra']
        );
        
        $key = $this->topicVoteLink . 0;
        $result = Storage::addData($key, $data);
        return $result;
    }

    /**
     * 添加投票选项
     *
     * @param unknown $info            
     * @return boolean
     */
    public function addVoteOptions($info)
    {   
        if (empty($info['tid']) || empty($info['vid']) || strlen($info['content']) < 1) {
            return false;
        }
        
        $data = array(
            'tid'       =>  $info['tid'],
            'vid'       =>  $info['vid'],
            'content'   =>  $info['content'],
            'vote_num'  =>  $info['vote_num'],
            'time'      =>  AC::SERVER_TIME,
            'extra'     =>  $info['extra']
        );
        
        $key = $this->topicVoteOptions . 0;
        $result = Storage::addData($key, $data);
        return $result;
    }
    /**
     * 添加投票详情
     * @param unknown $info
     * @return Ambigous <unknown, boolean>
     */
    public function addVoteLogs($oid, $uid, $author_uid, $tid, $vid)
    {   
        if (empty($oid)||empty($uid)||empty($author_uid)||empty($tid)|| empty($vid)){
	       return false;
    	}
	
        $data = array();
        $data['tid'] = $tid;
        $data['vid'] = $vid;
        $data['oid'] = $oid;
        $data['uid'] = $uid;
        $data['author_uid'] = $author_uid;
        $data['status'] = 0;
        $data['time'] = AC::SERVER_TIME;
        
        $key = $this->topicVoteLogs . $data['author_uid'];
        $result = Storage::addData($key, $data); 
        return $result;
    }

    /**
     * 获取投票Link信息
     * @param unknown $info
     * @return boolean
     */
    public function getVoteLinkInfo($tid)
    {   
        if (empty($tid)) {
            return array();
        }
    
        $where = "`tid` = $tid  AND `status` != 1";
        $key   = $this->topicVoteLink . 0;
        $result = Storage::getConditionData($key, $where);
        return $result;
    }
    
    /**
     * 获取投票选项信息
     * @param unknown $info
     * @return boolean
     */
    public function getVoteOptionsInfo($info)
    {   
        if (empty($info['tid'])) {
            return array();
        }
        
        $where = array('tid' => $info['tid']);
        $key = $this->topicVoteOptions . 0;
        $result = Storage::getConditionManyData($key, $where);
        return $result;
    }
    /**
     * 获取个人投票数量信息
     * @param unknown $info
     * @return boolean
     */
    public function getVoteLogsInfo($tid,$author_uid,$host_id)
    {  
        if (empty($tid)||empty($author_uid)|| empty($host_id)) return array();
	
        $data = array(
            'tid'   =>  $tid,
            'uid'   =>  $host_id
        );
        
        $key = $this->topicVoteLogs . $author_uid;
        $result = Storage::getConditionManyData($key, $data);
        return $result;
    }   
	
	/**
	 * 收藏话题
	 * @param unknown $info
	 * @return boolean
	 */
	public function addFavoriteTopic($info){
	    if (intval($info['uid']) < 1 || intval($info['tid']) < 1 ){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    if (intval($info['author_uid']) < 1 || strlen($info['author_name']) < 1){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    $data    =    array();
	    $data['uid']                 =    intval($info['uid']);
	    $data['topic_id']            =    intval($info['tid']);
	    $data['position']            =    intval($info['position']);
	    $data['title']               =    $info['title'];
	    $data['favorite_uid']        =    intval($info['author_uid']);
	    $data['favorite_name']       =    $info['author_name'];
	    $data['time']                =    AC::SERVER_TIME;
	    $data['status']              =    0;
	    
	    $key    =    $this->topicFavoriteKeyPrefix.$info['uid'];
	    $result =    Storage::addData($key, $data);
	    if (!$result){
	        return FALSE;
	    }
	    
	    return TRUE;
	}
	
	/**
	 * 获取收藏的话题列表
	 * @param unknown $info
	 * @return boolean|Ambigous <unknown, boolean, multitype:>
	 */
	public function getFavoriteTopic($info){
	    if (intval($info['uid']) < 1 ){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    $start     =    0;
	    $length    =    20;
	    isset($info['start'])    && $start    =    intval($info['start']);
	    isset($info['length'])   && $length   =    intval($info['length']);
	    if ($start < 0){
	        $start =    0;
	    }
	    
	    $where    =    " uid = ".$info['uid']." AND status = 0 AND position <= 1 AND type = ".AC::COMMENTOFGROUPTOPIOC;
	    if (intval($info['id']) > 0){
	        $where=    ' id < '.intval($info['id']).' AND uid = '.$info['uid'].' AND status = 0 AND position <= 1 AND type = '.AC::COMMENTOFGROUPTOPIOC;
	    }
	    $orderBy  =    ' ID DESC ';
	    $key      =    $this->topicFavoriteKeyPrefix.$info['uid'] .$this->slaveKey;
	    $result   =    $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
	    
	    return $result;
	}
	/**
	 * 获取收藏的话题列表
	 * @param unknown $info
	 * @return boolean|Ambigous <unknown, boolean, multitype:>
	 */
	public function getFavoriteTopicInfo($info){
	    if (empty($info['uid'])) return array();
	    
	    if (intval($info['tid']) < 1){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }

	    $where    =    " uid = ".$info['uid']." AND topic_id = ".$info['tid']." AND position <= 1 ";
	    if (intval($info['position']) > 1){
	        $where    =    " uid = ".$info['uid']." AND topic_id = ".$info['tid']." AND position = ".$info['position'];
	    } 
	    
	    $key      =    $this->topicFavoriteKeyPrefix.$info['uid'];
	    $result   =    Storage::getConditionData($key, $where);
	     
	    return $result;
	}
	/**
	 * 修改收藏状态
	 * @param unknown $info
	 * @return boolean
	 */
	public function setFavoriteTopic($info){
	    if (intval($info['uid']) < 1 || intval($info['tid']) < 1 ){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	     
	    $data    =    array();
	    $data['status']    =    0;
	    isset($info['status'])    &&    $data['status']    =    intval($info['status']);

	    $where    =    " uid = ".$info['uid']." AND topic_id = ".$info['tid']." AND position <= 1 ";
	    if (intval($info['position']) > 1){
	        $where    =    " uid = ".$info['uid']." AND topic_id = ".$info['tid']." AND position = ".$info['position'];
	    }
	     
	    $key    =    $this->topicFavoriteKeyPrefix.$info['uid'];
	    $result =    Storage::setConditionData($key, $data, $where);
	     
	    if ($result){
	        return TRUE;
	    }
	    return FALSE;
	}
	
	/**
	 * 取消收藏,将状态修改为1
	 * @param unknown $info
	 * @return boolean
	 */
	public function removeFavoriteTopic($info){
	    if (intval($info['uid']) < 1 || intval($info['tid']) < 1 ){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    $data    =    array();
	    $data['status']    =    1;
	    
	    $where    =    " uid = ".$info['uid']." AND topic_id = ".$info['tid']." AND position <= 1 ";
	    if (intval($info['position']) > 1){
	        $where    =    " uid = ".$info['uid']." AND topic_id = ".$info['tid']." AND position = ".$info['position'];
	    } 
	    
	    $key    =    $this->topicFavoriteKeyPrefix.$info['uid'];
	    $result =    Storage::setConditionData($key, $data, $where);
	    
	    if ($result){
	        return TRUE;
	    }
	    return FALSE;
	}
	
	/**
	 * 更新楼层喜欢数
	 * @param unknown $info
	 * @return boolean
	 */
	public function updateFavoriteCommentCount($info) {
	    if (intval($info['author_uid']) < 1 || intval($info['tid']) < 1 || intval($info['position']) < 1){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	
	    $data    =    array();
	    $data['favorites']    =    intval($info['favorites']);
	
	    $key    =    $this->topicCommentsKeyPrefix.$info['author_uid'];
	    $where  =    array('tid' =>$info['tid'], 'position' =>$info['position']);
	    $result =    Storage::limitConditionUpdate($key, $data, $where);
	
	    if (empty($result)) return false;
	    return true;
	}
	
	/**
	 * 点滴评论点赞
	 * @param unknown $info
	 * @return boolean
	 */
	public function addFavorOfDiaryComment ($uid, $tid, $position, $author_uid, $author_name) {
	    if (empty($uid) || empty($tid) || empty($position) || empty($author_uid) || strlen($author_name) < 1){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    $data    =    array(
	        'uid'          =>  intval($uid),
	        'topic_id'     =>  intval($tid),
	        'position'     =>  intval($position),
	        'favorite_uid' =>  intval($author_uid),
	        'favorite_name'=>  $author_name,
	        'status'       =>  0,
	        'type'         =>  AC::COMMENTOFTOPICDIARY,
	        'time'         =>  AC::SERVER_TIME
	    );

	    $key    =    $this->topicFavoriteKeyPrefix.$uid;
	    $result =    Storage::addData($key, $data);
	    if (empty($result)) return false;
	     
	    return true;
	}
	
	/**
	 * 取消点赞
	 * @param unknown $uid
	 * @param unknown $tid
	 * @param unknown $position
	 * @return boolean
	 */
	public function removeFavorOfDiaryComment ($uid, $tid, $position, $author_uid) {
	    return $this->updateFavorStatusOfDiaryComment($uid, $tid, $position, $author_uid, 'del');
	}
	
	/**
	 * 更新楼层的赞的状态
	 * @param unknown $uid
	 * @param unknown $tid
	 * @param unknown $position
	 * @param string $type
	 * @return boolean
	 */
	public function updateFavorStatusOfDiaryComment ($uid, $tid, $position, $author_uid, $type = 'add') {
	    
	    if (empty($uid) || empty($tid) || empty($author_uid) || empty($position)){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    $status = ($type == 'add') ? 0 : 1;
	    $data    =    array('status' => $status);
	    $where = array(
	        'uid'      =>  $uid,
	        'topic_id' =>  $tid,
	        'position' =>  $position,
	        'type'     =>  AC::COMMENTOFTOPICDIARY
	    );
	    
	    $key    =    $this->topicFavoriteKeyPrefix. $uid;
	    $result =    Storage::setConditionData($key, $data, $where);
	    if (empty($result)) return false;
	    
	    return true;
	}
	
	/**
	 * 更新点滴评论点赞数
	 * @param unknown $author_uid
	 * @param unknown $tid
	 * @param unknown $position
	 * @param string $type
	 * @return boolean
	 */
	public function updateFavoriteOfDiaryComment($author_uid, $tid, $position, $type = 'add') {
	    if (intval($author_uid) < 1 || intval($tid) < 1 || intval($position) < 1){
	        AC::$errorNo    =   'TPM001';
	        return  false;
	    }
	       
	    $data = $type === 'add' ? array('favorites' => 1) : array('favorites' => -1);

	    $key    =    $this->topicCommentsKeyPrefix.$author_uid;
	    $where  =    array('tid' => $tid, 'position' => $position);
	    $result =    Storage::limitConditionUpdate($key, $data, $where);
	
	    if (empty($result)) return false;
	    
	    return true;
	}
	
	/**
	 * 获取点滴评论点赞情况
	 * @param unknown $uid
	 * @param unknown $tid
	 * @param unknown $position
	 * @return boolean|Ambigous <multitype:, unknown, boolean>
	 */
	public function getFavorInfoOfDiaryComment ($uid, $tid, $position) {
	    if (empty($uid) || empty($tid) || empty($position)){
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    $where = array(
	        'uid'      =>  $uid,
	        'topic_id' =>  $tid,
	        'position' =>  $position,
	        'type'     =>  AC::COMMENTOFTOPICDIARY
	    );
	    
	    $key      =    $this->topicFavoriteKeyPrefix.$uid;
	    $result   =    Storage::getConditionData($key, $where);
	    
	    return $result;
	}
	
	public function removeTopic($info)/*{{{*/
	{
		$data   =   array('status' => 1);
		$key    =   $this->topicKeyPrefix. 0;
		$where  =   array('tid' => $info['tid']);
		$result =   Storage::setConditionData($key, $data, $where);
		if (empty($result))
		{
			AC::$errorNo    =   'TPM003';
			return FALSE;
		}
		return  $result;
	}/*}}}*/
    
	/**
	 * 获取话题信息
	 * @param unknown $info
	 * @return Ambigous <multitype:, unknown, boolean>
	 */
	public function getTopicInfo($info)/*{{{*/
	{
	    if (intval($info['tid']) < 1){
	        AC::$errorNo = 'TPM001';
	        return FALSE;
	    }
		$key    =   $this->topicKeyPrefix. 0;
		$where  =   array('tid' => $info['tid'],'status' => 0);
		$result =   Storage::getConditionData($key, $where);
		return  $result;
	}/*}}}*/
	
	public function getTopicInfoWithoutStatus($tid)/*{{{*/
	{
	    if ($tid < 1){
	        AC::$errorNo = 'TPM001';
	        return FALSE;
	    }
		$key    =   $this->topicKeyPrefix. 0;
		$where  =   array('tid' => $tid);
		$result =   Storage::getConditionData($key, $where);
		return  $result;
	}/*}}}*/
	
	/**
	 * 获取话题列表
	 * @param  $info
	 * @return boolean|NULL|Ambigous <unknown, boolean, multitype:>
	 */
	public function getTopicList($info)/*{{{*/
	{
		if (empty($info['gid']))
		{
			AC::$errorNo = 'TPM001';
			return FALSE;
		}
		
		$start  =   isset($info['start'])  ?   $info['start'] :   0;
		$length =   isset($info['length']) ?   $info['length']:   AC::TOPIC_LIST_DISPLAY_LEN;

		$tops      = array();
		if (empty($info['id']) && intval($start) < 1){
		    $topTopics = $this->getGroupTopTopics($info['gid']);
		    if (!empty($topTopics)){
		        $tops   = $topTopics['topics'];
		    }
		}
		
		if ($start  <   1)  $start  =   0;
		$now    =   AC::SERVER_TIME;
		$where  =   ' gid ='.$info['gid'].' AND status = 0 AND display = 0 AND time <= '.$now;
		// 看精华帖
		if (isset($info['show_digest']) && intval($info['show_digest']) > 0)
		{
			$tops   =   array();
			$where  =   ' gid ='.$info['gid'].' AND status = 0 AND time <= '.$now.' AND digest > 0';
		}
		$orderBy=   ' display DESC ,last_reply DESC ';
		// 按最新发布
		if (isset($info['new_topic']) && intval($info['new_topic']) > 0)
		{
			$where  =   ' gid ='.$info['gid'].' AND status = 0 AND display = 0 AND time <= '.$now;
			$orderBy=   ' time DESC ';
		}
			
		$key    =   $this->topicKeyPrefix. 0 .$this->slaveKey;
		$result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
		if (!empty($tops)){
            $result =   array_merge($tops, $result);
        }
		return  $result;
	}/*}}}*/
	
	/**
	 * 获取推荐列表
	 * @param unknown $start
	 * @param unknown $length
	 * @return Ambigous <unknown, boolean, multitype:>
	 */
	public function getRecommendTopicList($start, $length){
	    if ($start > self::RECOMMEND_TPOIC_RETURN_MAX_LEN) return [];
	    $cache_list = $this->diaryListCacheModel->getRecommendTopicList($start, $length - 1);
        if (!empty($cache_list)){
            $ret = [];
            foreach ($cache_list as $tid){
                $ret[] = $this->topicAddRedis(['tid'=>$tid]);
            }
            return $ret;
        }
	    $where  =   ' status = 0 AND recommend = 1 ';
	    $orderBy=   ' time DESC ';
	    	
	    $key    =   $this->topicKeyPrefix. 0 .$this->slaveKey;
	    $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, self::RECOMMEND_TPOIC_RETURN_MAX_LEN);
        $score = self::RECOMMEND_TPOIC_RETURN_MAX_LEN;
        foreach ($result as $item){
            isset($item['tid']) && $this->diaryListCacheModel->addRecommendTopic($item['tid'], $score);
            $score --;
        }
	    return  array_slice($result, 0, $length);
	}
	
	/**
	* 获取某个圈子全局置顶的话题
	* @param unknown $gid
	* @return multitype:|multitype:number
	*/
	private function getGroupTopTopics($gid){
	    $groupInfo = $this->groupInfoModel->groupAddRedis($gid);
	    if(empty($groupInfo) || empty($groupInfo['top_tids'])){
	        return array();
	    }
	    
	    $topTids = json_decode($groupInfo['top_tids'], true);
	    $data    = array();
	    foreach ($topTids as $val){
	        $tmp = $this->topicAddRedis(array('tid' => $val));
	        if (empty($tmp)){
	            continue;
	        }
	        $data['topics'][] = $tmp;
	    }
	    $data['counts'] = count($data['topics']);
	
	    return $data;
	}
	
	/**
	 * 获取置顶的话题
	 * @param unknown $info
	 */
	public function getStickTopicList($info){
	    if (empty($info['gid']))
	    {
	        AC::$errorNo = 'TPM001';
	        return FALSE;
	    }
	    $length =   AC::STICK_TOPIC_NUMS;
	    $start  =   0;
	    
	    $where  =   ' gid = '.intval($info['gid']).' AND display > 0 AND status = 0 ';
	    
	    $key    =   $this->topicKeyPrefix. 0;
	    $orderBy=   ' display DESC ';
	    $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
	    return  $result;
	}
	
	/**
	 * 获取精华帖
	 * @param unknown $length
	 * @return Ambigous <unknown, boolean, multitype:>
	 */
	public function getDigestTopics($length){
	    $start  =   0;
	    $where  =   ' digest > 0 AND status = 0 ';
	     
	    $key    =   $this->topicKeyPrefix. 0;
	    $orderBy=   ' last_reply DESC ';
	    $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
	    return  $result;
	}
	
	public function getMyTopics($info)/*{{{*/
	{
		if (empty($info['uid']))
		{
			AC::$errorNo = 'TPM001';
			return FALSE;
		}
		
		$start  =   isset($info['start'])  ?   $info['start'] :   0;
		$length =   isset($info['length']) ?   $info['length']:   AC::TOPIC_LIST_DISPLAY_LEN;
		if ($start  <   0)  $start  =   0;
		
		$where  =   array('uid' => $info['uid'],'status' => 0);
		if (intval($info['id']) > 0)   {
		    $start  =   '0';
		    $where  =   ' tid < '. $info['id'].' AND status = 0 AND uid = '.$info['uid'];
		}
		$orderBy=   ' tid DESC ';

		$key    =   $this->topicKeyPrefix. 0;
		$result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
		return  $result;
	}/*}}}*/
	
	public function getUserAllTopics($info)/*{{{*/
	{
		if (empty($info['uid']))
		{
			AC::$errorNo = 'TPM001';
			return FALSE;
		}
	
		$where  =   array('uid' => $info['uid'],'status' => 0);
		$key    =   $this->topicKeyPrefix. 0;
		$result =   Storage::getConditionManyData($key, $where);
		return  $result;
	}/*}}}*/
	
	public function getLastestTopic($uid, $start = 0, $length = 1) {
	    if (empty($uid)){
	        AC::$errorNo = 'TPM001';
	        return FALSE;
	    }
	    
	    $where  =   array('uid' => $uid);
	    
	    $key    =   $this->topicKeyPrefix. 0 .$this->slaveKey;
	    $orderBy=   " tid DESC ";
	    $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
	    return  $result;
	}
	
	public function getUnpublishedTopics($info)/*{{{*/
	{
		if (empty($info['uid']))
		{
			AC::$errorNo = 'TPM001';
			return FALSE;
		}
	
		$start  =   isset($info['start'])  ?   $info['start'] :   0;
		$length =   isset($info['length']) ?   $info['length']:   AC::TOPIC_LIST_DISPLAY_LEN;
		if ($start  <   0)  $start  =   0;
	
		$now   = AC::SERVER_TIME;
		$where = ' uid ='.$info['uid'].' AND status = 0 AND time > '.$now;
		if (intval($info['id']) > 0)   {
			$start  =   '0';
			$where  =   ' tid < '. $info['id'].' AND status = 0 AND uid = '.$info['uid'].' AND time > '.$now;
		}
		
		$orderBy=   ' tid DESC ';
		$key    =   $this->topicKeyPrefix. 0;
		$result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
		return  $result;
	}/*}}}*/
	
	public function countMyTopics($info)/*{{{*/
	{
		$sql      	= "select count(tid) from xxtShare.topic_0 where uid = ".$info['uid'].' AND status = 0';
		$keyClient  =   $this->topicKeyPrefix. 0;
		$result     =   Storage::fetchDbSentenceByClient($keyClient, $sql, 2);
		return  $result;
	}
	
	/**
	 * 将话题置顶/或加精
	 * @param unknown $info
	 * @return boolean|Ambigous <boolean, unknown>
	 */
	public function stickTopicInfo($info)/*{{{*/
	{
	    if (!isset($info['display']) && !isset($info['digest'])){
	        return FALSE;
	    }
	    $data['time']    =   AC::SERVER_TIME;
	    isset($info['display']) && $data['display']  =  $info['display'];
	    isset($info['digest'])  && $data['digest']   =  $info['digest'];
	    
	    $key    =   $this->topicKeyPrefix. 0;
	    $where  =   array('tid' => $info['tid'], 'gid' => $info['gid'], 'status' => 0);
	    $result =   Storage::setConditionData($key, $data, $where);
	    
	    return  $result;
	}/*}}}*/
	/**
	 * 设置话题最后回复时间
	 * @param unknown $info
	 * @return boolean|Ambigous <boolean, unknown>
	 */
	public function setTopicLastTime($info){
	    if (intval($info['uid']) < 1 || intval($info['tid']) < 1){
	        AC::$errorNo = 'TPM001';
			return FALSE;
	    }
	    $data['last_reply']          =   AC::SERVER_TIME;
	    $data['last_reply_uid']      =   $info['uid'];
	    $data['last_reply_username'] =   $info['nickname'];
	    $key    =   $this->topicKeyPrefix. 0;
	    $where  =   array('tid' => $info['tid'],'status' => 0);
	    $result =   Storage::setConditionData($key, $data, $where);
	    if (empty($result))
	    {
	        AC::$errorNo    =   'TOC022';
	        return  FALSE;
	    }
	    return  $result;
	}
	
	
	public function editTopic($info)/*{{{*/
	{
		$data['uid']    =   $info['uid'];
		$data['id']     =   $info['id'];
		$data['time']   =   AC::SERVER_TIME;
		
		isset($info['title'])          &&  $data['title']          =   substr($info['title'],0,AC::TOPIC_TITLE_LENGTH);
		isset($info['type'])           &&  $data['type']           =   $info['type'];
		isset($info['category'])       &&  $data['category']       =   $info['category'];
		
		$key    =   $this->topicKeyPrefix. 0;
		$where  =   array('id' => $info['id'],'uid' => $info['uid'],'status' => 0);
		$result =   Storage::setConditionData($key, $data, $where);
		if (empty($result))
		{
			AC::$errorNo    =   'TPM003';
			return  FALSE;
		}
		return  $result;
	}/*}}}*/
	
	/**
	* 修改投票表状态
	* @param unknown $info
	*/
	public function editVoteLink($vid,$status)
	{ 
	    if(empty($vid)) return false;  
	     
	    $data  = array('status' => $status);
	    $where = array('id'=>$vid);
	    $key = $this->topicVoteLink . 0;
	    $result = Storage::setConditionData($key, $data, $where);
	    return $result;
	}
	
	/**
	* 修改投票数量
	* @param unknown $Info
	* @return Ambigous <boolean, unknown>
	*/
	public function getUserVoteLogIds($tid,$author_uid, $uid){
	    if(empty($tid) || empty($uid)) return array();
	    
	    $where = array('uid' => $uid, 'tid' => $tid);
	    $key = $this->topicVoteLogs . $author_uid;
	    $result = Storage::getFieldsConditionManyData($key,'oid',$where);
	    return $result;
	}
	
	/**
	 * OptionVoteNnm自增1
	 * @param unknown $id
	 * @return boolean|Ambigous <boolean, unknown>
	 */
	public function increaseOptionVoteNum ($id) {
	    if(empty($id)) return false;
	    
	    $value = array('vote_num' => 1);
	    $where = array('id' => $id);
	    $key = $this->topicVoteOptions . 0;
	    $result = Storage::changeNumValueAdd($key, $value, $where);
	    return $result;
	}

	public function getOneTopic($info)/*{{{*/
	{
		if (empty($info['tid']))
		{
			AC::$errorNo = 'TPM001';
			return FALSE;
		}
		
		$key    =   $this->topicKeyPrefix. 0;
		$where  =   array('tid' => $info['tid'],'status' => 0);
		$result =   Storage::getConditionData($key, $where);
		if (empty($result)) return null;
		
		return  $result;
	}/*}}}*/
	
	public function countNewTopics($info)/*{{{*/
	{
		$daystart   = strtotime(date("Y-m-d"));
		$dayend     = $daystart+24*60*60;
		$sql        = "select count(tid) from xxtShare.topic_0 where gid = ".$info['gid'].' AND time >= '.$daystart.' AND time < '.$dayend;
		$keyClient  =  $this->topicKeyPrefix. 0;
		$result     =   Storage::fetchDbSentenceByClient($keyClient, $sql, 1);
		return  $result;
	}/*}}}*/
	
	#回应(评论)，responseNum加1
	public function addResponseNum($info)/*{{{*/
	{
		if (empty($info['tid']))
		{
			AC::$errorNo    =   'TPM001';
			return  FALSE;
		}

		$data   =   array();
		$data['response_num']   =   1;
	
		$key    =   $this->topicKeyPrefix. 0;
		$where  =   array('tid' => $info['tid']);
		$result =   Storage::changeNumValueAdd($key, $data, $where);
		if (empty($result))
		{
			AC::$errorNo    =   'TPM003';
			return  FALSE;
		}
	
		return  $result;
	}/*}}}*/
	
	#查看数，viewNum加1
	public function addViewNum($info)/*{{{*/
	{
	    if (empty($info['tid']))
	    {
	        AC::$errorNo    =   'TPM001';
	        return  FALSE;
	    }
	    
	    // 缓存加1
	    $topicInfo = $this->topicAddRedis($info);
	    if (!empty($topicInfo)){
	    	$this->diaryListCacheModel->changeTopicCount($info['tid'], 'view_num', 1);
	    }

	    $this->sendRedisSyncJob($info['tid']);
	    return TRUE;
	    
	    // 数据库加1
	    $data   =   array();
	    $data['view_num']   =   1;
	
	    $key    =   $this->topicKeyPrefix. 0;
	    $where  =   array('tid' => $info['tid']);
	    $result =   Storage::changeNumValueAdd($key, $data, $where);
	    
	    return  $result;
	}/*}}}*/

    #查看数，likeNum
    public function editLikeTimes($info)/*{{{*/
    {
        if (empty($info['tid']))
        {
            AC::$errorNo    =   'TPM001';
            return  FALSE;
        }

        // 缓存
        $topicInfo = $this->topicAddRedis($info);
        if($topicInfo['like_num'] < 1 && $info['like_num'] == -1) {
            return true;
        }
        $num = isset($info['like_num']) ? $info['like_num'] : 1;
        if (!empty($topicInfo)){
            $this->diaryListCacheModel->changeTopicCount($info['tid'], 'like_num', $num);
        }

        // 数据库
        $data   =   array();
        $data['like_num']   =   isset($info['like_num']) ? $info['like_num'] : 1;

        $key    =   $this->topicKeyPrefix. 0;
        $where  =   array('tid' => $info['tid']);
        $result =   Storage::changeNumValueAdd($key, $data, $where);

        return  $result;
    }
	
	private function sendRedisSyncJob ($tid) {
	    global $_G;
	    $host_name = empty($_G['serverHost']) ? "na" : $_G['serverHost'];
	    $data = array(
	        "task" => "topic",
	        "tid"  => strval($tid),
	        "source" => $host_name,
	        "time" => AC::SERVER_TIME
	    );
	     
	    RedisPool::doBackgroundGearmanJob("redis_sync", UK::JsonFormat($data), $tid);
	}

	// 从redis中获取话题信息,不存在则从数据库中获取并重新加入reids
	public function topicAddRedis($info){
		$data = $this->diaryListCacheModel->getTopicInfo( $info['tid'] );
		// 判断老缓存,没有附件则去更新缓存
		if (intval($data['upate_time']) < 1 || empty($data['gname']) || empty($data['gAvatar'])){
			$this->diaryListCacheModel->delTopicInfo($info['tid']);
			$data = null;
		}

		// 缓存没有则从数据库获取,然后再加入缓存中
		if (empty($data)){
			$topic  =  $this->getTopicInfo($info);
			if (empty($topic)){
				return  array();
			}
			$group    		=  $this->groupInfoModel->groupAddRedis($topic['gid']);

			$topic['gname']  	=  $group['name'];
			$topic['gAvatar']   =  $group['avatar'];
			$data     		=  $this->topicCacheData($topic);
			// 获取话题附件
			$tmp_atta['pid']        =    1;
			$tmp_atta['uid']        =    $topic['uid'];
			$tmp_atta['tid']        =    $topic['tid'];
			// 添加语音附件
			$attachment     	=    $this->attachmentModel->getAttachmentInfo($tmp_atta);
			$data['attachments'] 	=    $attachment['attachmentList'];
			$data['voiceList']   	=    $attachment['voiceList'];
	
			$data['upate_time']	    =    AC::SERVER_TIME;
			$this->diaryListCacheModel->addTopicCache($info['tid'], $data);
		}

		$CWservice = new CountWeightService('topic', $data['tid']);
		$numInfo = $CWservice->getCountWeight();
		$data['view_num'] += $numInfo['n1'];
		return $data;
	}
	
	// 构造话题存入redis缓存数据
	private function topicCacheData($info){
		$data = array();
		$data['id']			= strval($info['tid']);
		$data['tid']			= strval($info['tid']);
		$data['uid']			= strval($info['uid']);
		$data['gid']			= strval($info['gid']);
		$data['gname']			= strval($info['gname']);
		$data['gAvatar']        = strval($info['gAvatar']);
		$data['title']			= strval($info['title']);
		$data['vote'] 			= strval($info['vote']);
		$data['abbreviation']		= strval($info['abbreviation']);
		$data['category']		= intval($info['category']) > 0   ? strval($info['category'])   : '0';
		$data['highlight']		= strval($info['highlight']);
		$data['favtimes']		= intval($info['favtimes']) > 0   ? strval($info['favtimes'])   : '0';
		$data['sharetimes']		= intval($info['sharetimes']) > 0 ? strval($info['sharetimes']) : '0';
		$data['view_num']		= intval($info['view_num']) > 0   ? strval($info['view_num'])   : '0';
		$data['like_num']       = intval($info['like_num']) > 0   ? strval($info['like_num'])  : '0';
		$data['response_num']		= intval($info['response_num']) > 0 ? strval($info['response_num']) : '0';
		$data['recom_num']		= intval($info['recom_num']) > 0  ? strval($info['recom_num'])  : '0';
		$is_today 			    = UK::isToday($info['time']);
	
		$data['is_today']		= $is_today ? "1" : "0";
		$data['display']		= strval($info['display']);
		$data['digest']			= strval($info['digest']);
		$data['time']			= strval($info['time']);
		$data['status']			= strval($info['status']);
		$data['recommend']		= strval($info['recommend']);
		$data['last_reply']		= strval($info['last_reply']);
		$data['last_reply_uid']		= strval($info['last_reply_uid']);
		$data['last_reply_username']	= strval($info['last_reply_username']);
	
		return $data;
	}

	// 话题转发构造数据
	public function repeatTopicDataHandle($diaryInfo){
		strlen($diaryInfo['content']) < 1    &&  $diaryInfo['content']    =    '转发话题';
	
		// 判断话题是否存在
		$topic	=  $this->topicAddRedis(array('tid'=>$diaryInfo['contentId']));
		if (empty($topic)){
			AC::$errorNo = 'GRC002';
			return false;
		}
	
		$tmp = array(
			'url_type'	=>	$diaryInfo['contentType'],
			'url_type_id'	=>	$diaryInfo['contentId'],
			'url_type_uid'	=>	$diaryInfo['contentUid']
		);
		$diaryInfo['extras']['topic'][]	=	$tmp;
		$diaryInfo['extras']            =   json_encode($diaryInfo['extras']);
		return $diaryInfo;
	}
	
	/**
	 * 分享话题的数据处理
	 * @param unknown $param
	 * @return boolean|multitype:string
	 */
	public function structureTopicShareData ($param) {
	    // 解析action:"pinksns://group/topic?tid=1223&uid=2&gid=23"
	    $action = UK::getstr($param['data_action'], 512,'',1);
	    $urlarr = parse_url($action);
	    $action_pre = substr($action, 0, -strlen($urlarr['query']));
	    $parr = array();
	    parse_str($urlarr['query'],$parr);
	    
	    if ($action_pre == "pinksns://group/topic?"){
	        // 判断话题是否被删除
	        $topicInfo = $this->topicAddRedis(array('tid' => $parr['tid']));
	        if (empty($topicInfo)){
	            AC::$errorNo = 'GRC002';
	            return FALSE;
	        }
	    }
	    
	    // 参数说明
	    $url_struct = array(
	            'image'   => UK::getstr($param['data_image'], 512,'',1),
	            'action'  => $action,
	            'title'   => UK::getstr($param['data_title'], AC::TOPIC_TITLE_LENGTH,'',1),
	            'content' => UK::getstr($param['data_content'], 512,'',1)
	    );
	    
	    $result = array(
	            'msgType'    =>    '',
	            'msgData'    =>    ''
	    );
	    
	    if (!empty($url_struct['title']) && !empty($url_struct['content']) && !empty($url_struct['action'])){
	        $result = array(
	                'msgType'    =>    'share',
	                'msgData'    =>    UK::var_json_encode($url_struct)
	        );
	    }
	    return $result;
	}
	
	/**
	 * 获取最新话题列表
	 * @param unknown $category
	 * @param unknown $id
	 * @param unknown $start
	 * @param unknown $length
	 * @return Ambigous <unknown, boolean, multitype:>
	 */
	public function getTopicSquareList ($category, $id, $start, $length) {
	    $list = $this->diaryListCacheModel->getTopicSquareList( $category, $id, $start, $length );
	    if (empty($list) && $id < 1) {
	        $where  =   NULL;
	        $orderBy=   ' tid DESC ';
	        
	        // 按最新发布
	        if ($id > 0) {
	            $where  =   ' tid < '.$id;
	            $start  =   0;
	        }
	        	
	        $key    =   $this->topicKeyPrefix. 0 .$this->slaveKey;
	        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
	        
	        if (empty($result)) return array();
	        $list = array();
	        foreach ($result as $value) {
	            $list[] = $value['tid'];
	        }
	    }
	    
	    return  $list;
	}
	
	// ================================================ new =============================================
	/**
	 * 获取话题详情
	 * @param unknown $tid
	 * @param unknown $host_uid
	 * @return multitype:|Ambigous <mixed, multitype:string unknown NULL Ambigous <> >
	 */
	public function getTopicDetail ($tid) {
	    if (empty($tid)) {
	        return array();
	    }
	    
	    // 获取话题缓存
	    $topic = $this->diaryListCacheModel->getTopicInfo($tid);
	    if (empty($topic)) {
	        // 获取话题数据库数据
	        $topic = $this->getTopicDetailOfMysql($tid);
	        if ($topic) {
	            return array();
	        }
	        
	        // 获取话题附件
	        $attachment = $this->attachmentModel->getAttachmentInfo(array('pid' => 1, 'uid' => $topic['uid'], 'tid' => $topic['tid']));
	        $topic['attachments'] 	=    $attachment['attachmentList'];
	        $topic['voiceList']   	=    $attachment['voiceList'];
	        
	        // 重建缓存
	        $this->diaryListCacheModel->addTopicCache($tid, $topic);
	    }
	    
	    return $topic;
	}
	
	/**
	 * 获取话题详情(数据库)
	 * @param unknown $tid
	 * @return multitype:|Ambigous <multitype:, unknown, boolean>
	 */
	public function getTopicDetailOfMysql ($tid) {
	    if (empty($tid)){
	        return array();
	    }
	    
	    $key    =   $this->topicKeyPrefix. 0;
	    $where  =   array('tid' => $tid, 'status' => 0);
	    $result =   Storage::getConditionData($key, $where);
	    return  $result;
	}
	
	/**
	 * 话题列表
	 * @param unknown $gid
	 * @param unknown $digest
	 * @param unknown $new
	 * @param unknown $start
	 * @param unknown $length
	 * @return boolean|multitype:
	 */
	public function getTopicLists ($gid, $digest, $new, $start, $length) {
	    if (empty($gid)){
	        AC::$errorNo = 'TPM001';
	        return FALSE;
	    }
	    
	    // 置顶帖
	    $tops      = array();
	    if (empty($info['id']) && intval($start) < 1){
	        $topTopics = $this->getGroupTopTopics($gid);
	        if (!empty($topTopics)){
	            $tops   = $topTopics['topics'];
	        }
	    }
	    
	    // 默认
	    $now    =   AC::SERVER_TIME;
	    $orderBy=   ' display DESC ,last_reply DESC ';
	    $where  =   ' gid ='.$gid.' AND status = 0 AND display = 0 AND time <= '.$now;
	    
	    // 精华帖
	    if ($digest > 0) {
	        $tops   =   array();
	        $where  =   ' gid ='.$gid.' AND status = 0 AND time <= '.$now.' AND digest > 0';
	    }
	    
	    // 最新帖
	    if ($new > 0) {
	        $where  =   ' gid ='.$gid.' AND status = 0 AND display = 0 AND time <= '.$now;
	        $orderBy=   ' time DESC ';
	    }
	    	
	    $key    =   $this->topicKeyPrefix. 0 .$this->slaveKey;
	    $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
	    $result =   array_merge($tops, $result);
	    return  $result;
	}
	
	/**
	 * 组装话题详情返回数据
	 * @param unknown $autoModel
	 * @param unknown $topic
	 * @param number $host_uid
	 * @return unknown
	 */
	public function assembleTopicDetailData ($autoModel, $topic, $host_uid = 0) {
	    $data = array();
	    $data['id']			    = UK::intDeal($topic['id']);
	    $data['tid']			= UK::intDeal($topic['tid']);
	    $data['uid']			= UK::intDeal($topic['uid']);
	    $data['gid']			= UK::intDeal($topic['gid']);
	    $data['gname']			= strval($topic['gname']);
	    $data['gAvatar']        = strval($topic['gAvatar']);
	    $data['title']			= strval($topic['title']);
	    $data['content']		= strval($topic['abbreviation']);
	    $data['category']		= UK::intDeal($topic['category']);
	    $data['highlight']		= UK::intDeal($topic['highlight']);
	    $data['fav_num']		= UK::intDeal($topic['favtimes']);
	    $data['share_num']		= UK::intDeal($topic['sharetimes']);
	    $data['view_num']		= UK::intDeal($topic['view_num']);
	    $data['like_num']		= UK::intDeal($topic['like_num']);
	    $data['res_num']		= UK::intDeal($topic['response_num']);
	    $data['recom_num']		= UK::intDeal($topic['recom_num']);
	    $data['is_today']		= UK::isToday($topic['time']) ? "1" : "0";
	    $data['display']		= UK::intDeal($topic['display']);
	    $data['digest']			= UK::intDeal($topic['digest']);
	    $data['time']			= UK::intDeal($topic['time']);
	    $data['status']			= UK::intDeal($topic['status']);
	    $data['last_reply']		= UK::intDeal($topic['last_reply']);
	    $data['last_uid']		= UK::intDeal($topic['last_reply_uid']);
	    $data['recommend']      = UK::intDeal($topic['recommend']);
	    $data['last_user']		= strval($topic['last_reply_username']);
	    
	    // 判断是否收藏
	    $favor                  = 0;
	    if ($host_uid){
	        $favor				= $this->getFavoriteTopicInfo(array('uid' => $host_uid,'tid' => $topic['tid']));
	    }
	    $data['is_favor'] 		= !empty($favor)  ? '1' : '0';
	    
	    // 附件
	    $data['attachments']    = array();
	    $data['voiceList']      = array();
	    if (!empty($topic['attachments']) || !empty($topic['voiceList'])){
	        $data['attachments']    	=    $topic['attachments'];
	        $data['voiceList']      	=    $topic['voiceList'];
	    }
	    
	    // 判断老版本
	    $data['content'] = UK::tipsUpgrade($topic['voiceList'], $data['content'], 'voice');
	    
	    // 话题作者
	    $user			 = $autoModel->getUserInfo($topic['uid']);
	    $data['user']    = $autoModel->checkIsMe($user);
	    $data['user']['nickname']     	= $autoModel->getRemarkName($data['user']);
	    
	    return $data;
	}
	
	/**
	 * 获取点滴相关列表
	 * @param unknown $gid
	 * @param unknown $id
	 * @param unknown $start
	 * @param unknown $length
	 * @return boolean|multitype:
	 */
	public function getTopicDiarys ($gid, $id, $start, $length) {
	    if (empty($gid)){
	        AC::$errorNo = 'DTM001';
	        return FALSE;
	    }
        
	    $list = $this->diaryListCacheModel->getDiarysByTopicId($gid, $id, $length);
	    if (empty($list) && empty($id)) {
	        $key    =   $this->topicKeyPrefix. 0 .$this->slaveKey;
	        $where  =   " `gid` = ".$gid." AND `status` = ".AC::$diaryTopicCf['review_status']." AND `type` = ".AC::$diaryTopicCf['diary_topic'];
	        if ($id > 0) {
	            $where .= " AND `tid` < ".$id;
	        }
	         
	        $orderBy=   ' tid DESC ';
	        $list =   Storage::getConditionManyData($key, $where, $orderBy, $start, AC::LIMITLENGTHOFLIST);
	        if ($list) {
	            $this->addTopicDiaryToList($list, $gid);
	            $result = array_slice($list, 0, $length);
	        }
	    }else{
	        $result = $this->assembleTopicDiaryResults($list);
	    }
	    
	    return  $result;
	}
	
	/**
	 * 组装从缓存中获取的数据列表
	 * @param unknown $res
	 * @return multitype:multitype:unknown
	 */
	private function assembleTopicDiaryResults ($res) {
	    $result = array();
	    if ($res) {
	        foreach ($res as $bodyId) {
	            $tmp = array( 'type_id' => $bodyId);
	            $result[] = $tmp;
	        }
	    }
	    
	    return $result;
	}
	
	/**
	 * 重建缓存
	 * @param unknown $res
	 * @param unknown $gid
	 * @return boolean
	 */
	private function addTopicDiaryToList($res, $gid){
	    if($res && $gid > 0){
	        foreach ($res as $v){
	            $this->diaryListCacheModel->addTopicDiaryToList($gid, $v['type_id'], $v['time']);
	        }
	    }
	    
	    return true;
	}
	
	/**
	 * 添加点滴到话题
	 * @param unknown $info
	 * @return boolean|Ambigous <unknown, boolean, number, resource>
	 */
	public function addDiaryToTopic($info) {
	    if (empty($info['gid']) || empty($info['bodyId'])) {
	        AC::$errorNo = 'DTM001';
	        return FALSE;
	    }
	    
	    $data    =    array(
	        'uid'          =>  intval($info['uid']),
	        'gid'          =>  intval($info['gid']),
	        'title'        =>  strval($info['title']),
	        'abbreviation' =>  UK::getstr($info['content'], AC::TOPIC_ABBRE_LENGTH, '...',1),
	        'type'         =>  AC::$diaryTopicCf['diary_topic'],
	        'type_id'      =>  intval($info['bodyId']),
	        'status'       =>  2,
	        'time'         =>  AC::SERVER_TIME
	    );

	    $key    =   $this->topicKeyPrefix. 0;
	    $result =   Storage::addData($key, $data);
	    if (empty($result)) {
	        AC::$errorNo    =   'DTM002';
	        return  FALSE;
	    }
	    
// 	    if ($info['gid'] >= AC::$diaryTopicCf['start_id']) {
	        $this->diaryListCacheModel->addTopicDiaryToList($info['gid'], $info['bodyId']);
// 	    }
	    
	    return  $result;
	}
	
	public function updateTimeOfDiarybyTopic ($bodyId, $topic_id) {
        if (empty($bodyId) || empty($topic_id)) return false;
		$diaryListModel = new DiaryListModel();
		$top_level = $diaryListModel->checkIsTopDiaryOfDetail($topic_id, $bodyId);
		if ($top_level>0)return true;//如果是置顶的话题，忽略更新时间
        
        $diaryTopic = $this->groupInfoModel->getDiaryTopicInfo($topic_id);
        if ($diaryTopic && $diaryTopic['status'] != AC::$diaryTopicCf['delete_status']) {
            $this->diaryListCacheModel->addTopicDiaryToList($topic_id, $bodyId);
        }
        
        return true;
    }
	
	/**
	 * 删除点滴
	 * @param unknown $type_id
	 * @return boolean|Ambigous <boolean, unknown, number, resource>
	 */
	public function removeTopicDiary($type_id) {
	    if (empty($type_id)) return false;
	    
	    $data   =   array('status' => 1);
	    $key    =   $this->topicKeyPrefix. 0;
	    $where  =   array('type' => AC::$diaryTopicCf['diary_topic'], 'type_id' => $type_id);
	    $result =   Storage::setConditionData($key, $data, $where);

	    return  $result;
	}

    /**
     * 更新点滴话题
     * @param  $type_id
     * @param  $topic_id
     * @return boolean|Ambigous <boolean, unknown, number, resource>
     */
    public function updateTopicDiary($type_id, $topic_id) {
        if (empty($type_id)) return false;

        $data   =   array('gid' => $topic_id);
        $key    =   $this->topicKeyPrefix. 0;
        $where  =   array('type' => AC::$diaryTopicCf['diary_topic'], 'type_id' => $type_id);
        $result =   Storage::setConditionData($key, $data, $where);

        return  $result;
    }

    public function getCountWeight($type, $type_id)
    {
        $key    =   $this->topicCountWeight. 0 .$this->slaveKey;
        $where  =   array('type_id' => $type_id, 'type' => $type);
        $result =   Storage::getConditionData($key, $where);
        if (empty($result)) return array();

        return  $result;
    }

    public function getCountWeightList($type, $id)
    {
        $where = ' type = '.$type;
        if ($id > 0)   {
            $where  =   ' id < '. $id.' AND '.$where;
        }

        $orderBy=   ' id DESC ';
        $key    =   $this->topicCountWeight. 0 .$this->slaveKey;
        $result =   Storage::getConditionManyData($key, $where, $orderBy, 0, AC::TOPIC_LIST_DISPLAY_LEN);
        return  $result;
    }

    public function initCountWeight($type, $type_id, $param)
    {
        $data    =    array(
            'type'    =>  intval($type),
            'type_id'    =>  intval($type_id),
            'n1'    =>  intval($param['n1']),
            'n2'    =>  intval($param['n2']),
            'n3'    =>  intval($param['n3']),
            'n4'    =>  intval($param['n4']),
            'n5'    =>  intval($param['n5']),
            'n6'    =>  intval($param['n6']),
            'n7'    =>  intval($param['n7']),
            'n8'    =>  intval($param['n8']),
            'n9'    =>  intval($param['n9']),
            'update_at'    =>  AC::SERVER_TIME,
            'create_at'    =>  AC::SERVER_TIME,
        );

        $key    =   $this->topicCountWeight. 0;
        $result =   Storage::addData($key, $data);
        if (empty($result)) return  false;

        return $result;
    }

    public function editCountWeight($type, $type_id, $param)
    {
        $data = array('update_at' => AC::SERVER_TIME);
        isset($param['n1']) && $data['n1'] = intval($param['n1']);
        isset($param['n2']) && $data['n2'] = intval($param['n2']);
        isset($param['n3']) && $data['n3'] = intval($param['n3']);
        isset($param['n4']) && $data['n4'] = intval($param['n4']);
        isset($param['n5']) && $data['n5'] = intval($param['n5']);
        isset($param['n6']) && $data['n6'] = intval($param['n6']);
        isset($param['n7']) && $data['n7'] = intval($param['n7']);
        isset($param['n8']) && $data['n8'] = intval($param['n8']);
        isset($param['n9']) && $data['n9'] = intval($param['n9']);
        if (count($data) < 2) return false;

        $key    =   $this->topicCountWeight. 0;
        $where  =   array('type_id' => $type_id, 'type' => $type);
        $result =   Storage::setConditionData($key, $data, $where);
        if (empty($result)) return  false;

        return $result;
    }
}